package cn.lll.vuehouse.service.impl;

import cn.lll.vuehouse.DTO.SaleDTO;
import cn.lll.vuehouse.FORM.SaleBackForm;
import cn.lll.vuehouse.FORM.SaleForm;
import cn.lll.vuehouse.common.ResponseResult;
import cn.lll.vuehouse.common.ResponseStatus;
import cn.lll.vuehouse.entity.Customer;
import cn.lll.vuehouse.entity.Goods;
import cn.lll.vuehouse.entity.Sales;
import cn.lll.vuehouse.mapper.CustomerMapper;
import cn.lll.vuehouse.mapper.GoodsMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.lll.vuehouse.entity.Salesback;
import cn.lll.vuehouse.service.SalesbackService;
import cn.lll.vuehouse.mapper.SalesbackMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author asus-pc
* @description 针对表【bus_salesback】的数据库操作Service实现
* @createDate 2023-09-01 11:34:05
*/
@Service
public class SalesbackServiceImpl extends ServiceImpl<SalesbackMapper, Salesback>
    implements SalesbackService{

    @Autowired
    private SalesbackMapper salesbackMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public ResponseResult getList(SaleDTO saleDTO) {
        ResponseResult responseResult = new ResponseResult();
        Page<Salesback> salesPage = new Page<>(saleDTO.getCurrentPage(), saleDTO.getLimit());

        // 根据商品名查询商品id
        LambdaQueryWrapper<Goods> lambda1 = new QueryWrapper<Goods>().lambda();
        lambda1.like(StringUtils.isNotBlank(saleDTO.getName()),Goods::getGoodsname,saleDTO.getName());
        List<Integer> ids = goodsMapper.selectList(lambda1).stream().map(goods -> goods.getId()).collect(Collectors.toList());

        LambdaQueryWrapper<Salesback> lambda = new QueryWrapper<Salesback>().lambda();
        lambda.in(Salesback::getGoodsid,ids);
        salesbackMapper.selectPage(salesPage,lambda);
        List<Salesback> records = salesPage.getRecords();
        long total = salesPage.getTotal();

        List<SaleBackForm> SaleBackForms = records.stream().map(sales -> {
            SaleBackForm saleForm = new SaleBackForm();
            BeanUtils.copyProperties(sales,saleForm);
            // 获取商品名
            Goods goods = goodsMapper.selectById(sales.getGoodsid());
            String goodsname = goods.getGoodsname();
            // 获取销售商名
            Customer customer = customerMapper.selectById(sales.getCustomerid());
            String customername = customer.getCustomername();
            saleForm.setCustomer(customername);
            saleForm.setGoods(goodsname);
            return saleForm;
        }).collect(Collectors.toList());

        HashMap<String, Object> map = new HashMap<>();
        map.put("pageData",SaleBackForms);
        map.put("total",total);
        responseResult.success(ResponseStatus.SUCCESS,map);
        return responseResult;
    }
}




